/************************************************************************

							Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							  heroes4.cpp

	$Header: /game/heroes4.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <direct.h>

#include <algorithm>
#include <strstream>

#include "animated_window.h"
#include "animation_cache.h"
#include "button_cache.h"
#include "convert_16_bit.h"
#include "enum_operations.h"
#include "external_string.h"
#include "font_cache.h"
#include "game_application.h"
#include "idle_processor.h"
#include "main_menu.h"
#include "music.h"
#include "options.h"
#include "paint_surface.h"
#include "pixel_24.h"
#include "platform.h"
#include "player.h"
#include "ptr_cache.h"
#include "resource.h"
#include "resource_dir.h"
#include "string_table.h"
#include "windows_game.h"

#include "debug_message.h"
#include "format_string.h"
#include "spell.h"
#include "spell_properties.h"
#include "spell_icon.h"
#include "town_type.h"

#include "sound_cache.h"

// #define k_useless_serialized_version true

t_button_cache g_large_button( "large_button" );
t_button_cache g_ok_button( "button.ok" );
t_button_cache g_cancel_button( "cancel_button" );
t_button_cache g_checkbox( "checkbox" );
t_animation_cache	g_sample_cursor( "sample_cursor" );

extern int g_window_width;
extern int g_window_height;

namespace
{

class t_gray_window : public t_window
{
public:
	t_gray_window( t_screen_rect const& rect, t_window* parent = 0 ) 
		: t_window( k_no_transparency, parent) 
	{
		init( rect );
	}
	virtual void paint( t_paint_surface& paint_surface );
};


class t_game : public t_windows_game
{
public:
	t_game();
	~t_game();

	void free_materials();

	virtual std::string get_main_window_caption() const;
	virtual t_uint32    idle();
	virtual void        initialize();
	virtual bool        menu_click( int id );
protected:
	t_counted_ptr<t_gray_window>  m_frame_window;
};


}; // end unnamed namespace

void t_gray_window::paint( t_paint_surface& paint_surface )
{
	t_abstract_bitmap16_ptr bitmap = paint_surface.get_bitmap();
	t_pixel_24              pixel;
	t_screen_rect           rect = paint_surface.get_rect();

	pixel.red = 0;
	pixel.green = 0;
	pixel.blue = 0;

	t_uint16 pixel_16 = convert_to_16_bit( pixel );

	int x;
	int y;
	int       increment = bitmap->get_pitch() - rect.width() * 2;
	t_uint16* ptr = bitmap->get_data_ptr();

	for (y = 0; y < rect.height(); y++)
	{
		for (x = 0; x < rect.width(); x++)
		{
			*ptr++ = pixel_16;
		}
		ptr = bitmap->byte_increment( ptr, increment );
	}
}

t_game::t_game() : t_windows_game( "Heroes4", IDI_HEROES4 )
{
}

t_game::~t_game()
{
}

std::string t_game::get_main_window_caption() const
{
	return "Heroes 4";
}

// --------------------------------------------------------------------------------
// add free resources
// --------------------------------------------------------------------------------
void t_game::free_materials()
{
}

// --------------------------------------------------------------------------------
// handle a menu click
// --------------------------------------------------------------------------------
bool t_game::menu_click( int id )
{
	switch (id)
	{
		case IDM_EXIT:
			close_game();
			return true;
	}
	return false;
}

t_uint32 t_game::idle()
{
	t_uint32 next_idle_time;
	t_uint32 next_run_time;

	next_idle_time = t_game_application::idle();
	next_run_time  = t_idle_processor::run();

	if (next_run_time < next_idle_time) {
		return next_run_time;
	} else {
		return next_idle_time;
	}
}


// --------------------------------------------------------------------------------
// initialize
// --------------------------------------------------------------------------------
static t_external_string const k_text_serial_failure( "prerelease_register.misc" );
static t_external_string const k_text_cheat_menu( "access_cheat_menu.cheats" );

void t_game::initialize()
{
	t_window_ptr window;

	load_options();	

	//== writen by sunjun ==//
	// add_resource_file( "data/new.h4r" );
	// add_resource_file( "data/updates.h4r" );
	add_resource_file( "data/text.h4r" );
	add_resource_file( "data/music.h4r" );
	add_resource_file( "data/movies.h4r" );
	add_resource_folder( "data" );
	if (!add_resource_file( "data/heroes4.h4r" ))
	{
		fatal_error( "Unable to open data/heroes4.h4r" );
		exit(0);
	}

	t_pointer_cache< t_string_table > string_table_cache( "text" );
	t_pointer_cache< t_string_table > movie_string_table_cache( "intro voice over text" );
	t_pointer_cache< t_string_table > credits_string_table_cache( "credits" );

	t_external_string::add_table( *string_table_cache.get() );
	t_external_string::add_table( *movie_string_table_cache.get() );
	t_external_string::add_table( *credits_string_table_cache.get() );

	// this is a once-off shutdown for pre-release code
#ifdef k_useless_serialized_version
	fatal_error( k_text_serial_failure );
	exit(0);
#endif

	std::string cheat_text;

	if (get_command_line_argument( k_text_cheat_menu, cheat_text ))
	{		
		set_show_cheat_menus( true );
	}

	t_screen_point screen_size = get_desktop_size();

	m_frame_window = new t_gray_window(t_screen_rect(0, 0, g_window_width, g_window_height));

	t_mouse_window* cursor = new t_mouse_window;
	t_window*       animation = new t_animated_window( g_sample_cursor.get(), 
		                                               t_screen_point(0,0), 100, cursor );
	m_frame_window->set_cursor( cursor );
	
	set_resolution(g_window_width, g_window_height, false);

	initialize_platform();

	// Play the logo cinematics
	t_sound_cache music = get_music_playing();
	stop_music();

	play_music( music );

	t_main_menu* main_menu = new t_main_menu( m_frame_window );	
}


static t_game g_game;

